package fun.formoji.commons.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * <b><code>RestResponse</code></b>
 * <p/>
 * Description
 * <p/>
 * <b>Creation Time:</b> 2019/9/12 0:15.
 *
 * @author hshe
 */
@Data
@NoArgsConstructor
@Slf4j
public class RestResponse<T> implements Serializable {

    private static final long serialVersionUID = 1052605236159056373L;

    public RestResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private int code;
    private String msg;
    private T data;


    public static <T> RestResponse<T> build(int code, String msg, T data) {
        return new RestResponse<T>(code, msg, data);
    }

    public static <T> RestResponse<T> build(ServiceCode serviceCode, String msg, T data) {
        return new RestResponse<T>(serviceCode.getCode(), msg, data);
    }

    public static <T> RestResponse<T> build(ServiceCode serviceCode) {
        return new RestResponse<T>(serviceCode.getCode(), serviceCode.getMsg(), null);
    }

    public static <T> RestResponse<T> build(ServiceCode serviceCode, T data) {
        return new RestResponse<T>(serviceCode.getCode(), serviceCode.getMsg(), data);
    }


    public static <T> RestResponse<T> ok() {
        return build(ServiceCode.SUCCESS);
    }

    public static <T> RestResponse<T> ok(T data) {
        return build(ServiceCode.SUCCESS, data);
    }

    public static <T> RestResponse<T> ok(String msg, T data) {
        return build(ServiceCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> RestResponse<T> fail() {
        return build(ServiceCode.FAILURE);
    }

    public static <T> RestResponse<T> fail(T data) {
        return build(ServiceCode.FAILURE, data);
    }

    public static <T> RestResponse<T> fail(String msg, T data) {
        return build(ServiceCode.FAILURE.getCode(), msg, data);
    }

    public static <T> RestResponse<T> invalid() {
        return build(ServiceCode.PARAM_IS_INVALID);
    }


    public static <T> RestResponse<T> invalid(T data) {
        return build(ServiceCode.PARAM_IS_INVALID, data);
    }

    public static <T> RestResponse<T> invalid(String msg, T data) {
        return build(ServiceCode.PARAM_IS_INVALID.getCode(), msg, data);
    }

    public static <T> RestResponse<T> fallback() {
        return build(ServiceCode.INTERFACE_INNER_INVOKE_ERROR);
    }

}
